<?php

namespace app\controllers\supplier\uni\finance;

use app\controllers\supplier\base\AdminController;
use app\models\order\Order;
use app\models\pay\Pay as Model;

class PayController extends AdminController
{
    public function actionList()
    {
        // todo 进帐流水可能存在用余额支付
        // $sn_list = Order::find()
        //     ->where(['supplier_id' => $this->user->id])
        //     ->select(['sn'])
        //     ->column();

        $query = Order::find()
            ->where(['supplier_id' => $this->user->id])
            ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']]);

        $sn = $this->post('sn');
        $user_id = $this->post('user_id');
        $export = $this->post('export');

        $start = $this->post('start');
        $end = $this->post('end');

        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $sort = $this->post('sort');

        // $query = Model::find()
        //     ->where(['status' => 1])
        //     ->andWhere(['sn' => $sn_list]);

        if ($sort) {
            $prop = substr($sort, 0, 1);
            $prop = $prop == '-' ? SORT_DESC : SORT_ASC;
            $order = substr($sort, 1);
            $query->orderBy([$order => $prop]);
        } else {
            $query->orderBy(['id' => SORT_DESC]);
        }

        if ($user_id) {
            $query->andWhere(['user_id' => $user_id]);
        }
        if ($sn) {
            $query->andWhere(['like', 'sn', $sn]);
        }

        if ($start) {
            $query->andWhere(['>=', 'DATE_FORMAT(create_time, "%Y-%m-%d")', date('Y-m-d', strtotime($start))]);
        }
        if ($end) {
            $query->andWhere(['<=', 'DATE_FORMAT(create_time, "%Y-%m-%d")', date('Y-m-d', strtotime($end))]);
        }

        // export
        if ($export) {
            $list = $query->asArray()->all();
            return $this->success($this->getList($list));
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->limit($page_size)
            ->asArray()
            ->all();

        $list = $this->getList($list);

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
        ]);

    }

    private function getList($list)
    {
        foreach ($list as &$item) {
            $text = '';
            $item['amount'] = $item['total_pay'];
            if ($item['use_balance'] > 0 && $item['use_money'] > 0) {
                $text = '余额 + 现金';
            } else if ($item['use_balance'] > 0) {
                $text = '余额';
            } else if ($item['use_money'] > 0) {
                $text = '现金';
            }
            $item['pay_type_text'] = $text;
        }

        return $list;
    }
}